package com.fjnu.dao.impl;

import com.fjnu.dao.UserDao;
import com.fjnu.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.*;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private Connection connection;

    @Override
    public int insert(User user) {
        String sql = "INSERT INTO [user] (user_id, username, password) VALUES (?, ?, ?)";

        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, user.getUserId());
            pstmt.setString(2, user.getUsername());
            pstmt.setString(3, user.getPassword());

            int result = pstmt.executeUpdate();
            System.out.println("插入用户成功，影响行数: " + result);
            return result;
        } catch (SQLException e) {
            System.out.println("插入用户失败: " + e.getMessage());
            return 0;
        }
    }

    @Override
    public int delete(String userId) {
        String sql = "DELETE FROM [user] WHERE user_id = ?";

        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, userId);
            int result = pstmt.executeUpdate();
            System.out.println("删除用户成功，影响行数: " + result);
            return result;
        } catch (SQLException e) {
            System.out.println("删除用户失败: " + e.getMessage());
            return 0;
        }
    }

    public User login(String username, String password) {
        return selectByUsernameAndPassword(username, password);}

    @Override
    public int updateById(String userId, User newUser) {
        // 简化：只更新昵称
        String sql = "UPDATE [user] SET nickname = ? WHERE user_id = ?";

        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, newUser.getNickname());
            pstmt.setString(2, userId);

            int result = pstmt.executeUpdate();
            System.out.println("更新用户成功，影响行数: " + result);
            return result;
        } catch (SQLException e) {
            System.out.println("更新用户失败: " + e.getMessage());
            return 0;
        }
    }

    @Override
    public int updatePasswordById(String userId, String oldPassword, String newPassword) {
        String sql = "UPDATE [user] SET password = ? WHERE user_id = ? AND password = ?";

        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, newPassword);
            pstmt.setString(2, userId);
            pstmt.setString(3, oldPassword);

            int result = pstmt.executeUpdate();
            System.out.println("更新密码成功，影响行数: " + result);
            return result;
        } catch (SQLException e) {
            System.out.println("更新密码失败: " + e.getMessage());
            return 0;
        }
    }

    @Override
    public User selectById(String userId) {
        String sql = "SELECT * FROM [user] WHERE user_id = ?";

        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, userId);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                User user = new User();
                user.setUserId(rs.getString("user_id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                return user;
            }
        } catch (SQLException e) {
            System.out.println("查询用户失败: " + e.getMessage());
        }
        return null;
    }

    @Override
    public User selectByUsernameAndPassword(String username, String password) {
        String sql = "SELECT * FROM [user] WHERE username = ? AND password = ?";

        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                User user = new User();
                user.setUserId(rs.getString("user_id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                return user;
            }
        } catch (SQLException e) {
            System.out.println("登录验证失败: " + e.getMessage());
        }
        return null;
    }

    @Override
    public int selectCount() {
        String sql = "SELECT COUNT(*) FROM [user]";

        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                int count = rs.getInt(1);
                System.out.println("用户总数: " + count);
                return count;
            }
        } catch (SQLException e) {
            System.out.println("统计用户数量失败: " + e.getMessage());
        }
        return 0;
    }
}